package com.sengled.cloud.biz.union.mgt.common.util;
import org.springframework.session.ExpiringSession;
import org.springframework.session.FindByIndexNameSessionRepository;

import java.util.List;
import java.util.Map;

public class SessionUtil {

	public static void invalidateSession(FindByIndexNameSessionRepository<ExpiringSession> sessionRegistry, String username){
		Map<String, ExpiringSession> sessionsInfo = sessionRegistry.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username);
		for (Map.Entry<String, ExpiringSession> entry: sessionsInfo.entrySet()) {
			String key = entry.getKey();
			ExpiringSession value = entry.getValue();
			sessionRegistry.delete(key);
			value.removeAttribute("user");
		}
	}

	public static void batchInvalidateSession(FindByIndexNameSessionRepository<ExpiringSession> sessionRegistry, List<String> usernames){
		usernames.forEach(un -> {
			invalidateSession(sessionRegistry,un);
		});
	}
}
